<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%@ page import = "java.sql.*" %>

<%@ page import = "com.shopping.*"%>


<%

String actionName = request.getParameter("action");

if(actionName!=null && actionName.trim().equals("register")){
	String username = request.getParameter("username");
	
	String password = request.getParameter("password");
	
	String phone = request.getParameter("phone");
	
	String addr = request.getParameter("addr");
	
	User user = new User();
	user.setUsername(username);
	user.setPassword(password);
	user.setPhone(phone);
	user.setAddr(addr);
	user.setRdate(new Timestamp(System.currentTimeMillis()));
	
	user.save();
	
	out.println("Congratulations!register ok!");
}
 %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户注册</title>

<script type="text/javascript" src="scripts/regcheckdata.js"></script>

<script type="text/javascript">


function validateusername(){

	
	var xmlhttp;
	
	if(window.XMLHttpRequest){
	
	 	xmlhttp = new XMLHttpRequest();
	 	
	}else {
		xmlhttp = new ActivexObject("Microsoft.XMLHTTP");
	}
	
	var useridcomponent = document.getElementById("userid");
	var url ="validate.jsp?username="+escape(useridcomponent.value);
	xmlhttp.open("GET",url,true);
	
	
	xmlhttp.onreadystatechange = function(){
		var status = xmlhttp.readyState;
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			var responseText = xmlhttp.responseText;
			var msg = xmlhttp.responseXML.getElementsByTagName("msg")[0];
			var msgvalue = msg.childNodes[0].nodeValue;
			setMsg(msgvalue);
		}
		
	}
	
	//将请求发送到服务器
	//如果,填写send(String content),发送的内容仅适用于post方法
	xmlhttp.send();
}


function setMsg(msg){
	
	if(msg == "invalid"){
		document.getElementById("msgdiv").innerHTML = "<font color ='red'>用户名无效</font>";
	}else{
		document.getElementById("msgdiv").innerHTML = "";
	}
	

}

</script>
</head>
<body>
	
	<!-- action="register.jsp"提交到自身来 -->
	<form action="register.jsp" method="post" onsubmit="return checkregcheckdata()" name="regform">
		
		<input type="hidden" name="action" value="register">
	
		<table width="760" align="center" border="2">
			<tr>
				<td colspan="2" align="center"> 
					用户注册 
				</td>
				
			</tr>
			
			<tr>
				<td>
					用户名
				</td>
				<td>
					<input type="text" size="15" maxlength="15" name="username" onblur="validateusername()" id="userid">
					
					<div id="msgdiv"></div>
					
				</td>
			</tr>
			
			<tr>
				<td>
					密码
				</td>
				<td>
					<input type="password" size="15"  maxlength="15" name="password">
				</td>
			</tr>
			
			
			<tr>
				<td>
					确认密码
				</td>
				<td>
					<input type="password" size="15"  maxlength="15" name="password2">
				</td>
			</tr>
			
			<tr>
				<td>
					电话
				</td>
				<td>
					<input type="text" size="15"  maxlength="15" name="phone">
				</td>
			</tr>
			
			<tr>
				<td>
					地址
				</td>
				<td>
					<textarea rows="5" cols="30" name="addr"></textarea>
				</td>
			</tr>
			
			<tr>
				<td colspan="2">
					<input type="submit" value="提交">
					<input type="button" value="清空" onclick="clearregform()">
				</td>
				
			</tr>
		</table>
	
	</form>
</body>
</html>